/**
 * Created by 静龄 on 2015/11/9.
 */


!
    function(){
        var searchpage=new Rpage.pageObj();
        var searchkey="";
        function getdata(){
            cleantable($(".searchtable"));
            $.post(NEWS_LIST,{"key":searchkey,"page":searchpage.pagenum,"pageCount":searchpage.perpage,"task_id":args.id,"task_type":2},function(data){
                data= $.parseJSON(data);
                if(data.code==1){
                    var datalength=data.result.length;
                    for(var i=0;i<datalength;i++){
                        $(".searchtable").children("tbody").append( '<tr>' +
                            '<td><span class="R_read R_switch_a" data-read-id="'+data.result[i].id+'" data-on="已读" data-off="未读" data-ison="'+(data.result[i].uid?"true":"false")+'"></span></td>' +
                            '<td>'+(Rcheck.changedate(data.result[i].time_publish))+'</td>' +
                            '<td class="table_keep">'+(data.result[i].title_content||"")+'</td>' +
                            '<td>'+data.result[i].text_content+'</td>' +
                            '<td class="table_keep">'+(function(data,i){
                                $.post(GET_TASK_BY_ID,{"id":data.result[i].task_id},function(data2){
                                    data2= $.parseJSON(data2);
                                    if(data2.result.length==0){
                                        $(".searchtable").children("tbody").children("tr:eq("+i+")").children("td:eq(4)").html("来源不存在或已被删除");
                                    }else{
                                        $(".searchtable").children("tbody").children("tr:eq("+i+")").children("td:eq(4)").html(data2.result[0].task_name);
                                    }
                                });

                            })(data,i)+'</td>' +
                            '<td class="tableURL"><a  target="_blank" href="'+data.result[i].url_source.substring(data.result[i].url_source.indexOf("-->")-(-3))+'">URL <i class="fa fa-angle-right"></i></a></td>' +
                            '</tr>');
                    }
                    searchpage.setallpagenum(data.count);
                    $(".R_read").each(function(index,e){
                        Rswitch.setswitch($(this));
                    });
                }
            });
        }
        getdata();


        $(".searchbox").children("button").click(function(e){
            searchpage.pagenum=1;
            searchkey=($(".searchbox").children("input").val());
            getdata();
            return false;
        });

        searchpage.setpagediv($(".table_page"));
        searchpage.setpagechange(function(e){
            getdata();
        });
        $(".allread").click(function(e){
            $(".R_switch_a").each(function(index,e){
                var jqobj=$(this);
                if(jqobj.attr("data-ison")=="false"){
                    jqobj.click();
                }
                $.post(UPDATE_ANSWER,{"ccid":jqobj.attr("data-read-id"),"isRead":1});
            });
        });

    }();